package org.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.CourseMapper;
import org.example.model.table.Course;
import org.example.model.table.User;
import org.example.model.vo.CourseVo;
import org.example.service.CourseService;
import org.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseMapper courseMapper;

    @Override
    public Result addCourse(Course course) {
        int insert = courseMapper.insert(course);
        return insert == 1 ?
                Result.ok().data("msg", "插入成功") :
                Result.error().data("msg", "插入失败");
    }

    @Override
    public Result deleteCourseById(Long id) {
        int i = courseMapper.deleteById(id);
        return i == 1 ?
                Result.ok().data("msg", "删除成功") :
                Result.error().data("msg", "删除失败");
    }

    @Override
    public Result changeCourse(Course course) {
        int i = courseMapper.updateById(course);
        return i == 1 ?
                Result.ok().data("msg", "修改成功") :
                Result.error().data("msg", "修改失败");
    }

    @Override
    public Result getCourseById(Long id) {
        Course course = courseMapper.selectById(id);
        return Result.ok().data("course", course);
    }

    @Override
    public Result getCoursePage(Integer page, Integer size, CourseVo courseVo) {
        MPJLambdaWrapper<Course> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAs(Course::getId, CourseVo::getId)
                .selectAs(Course::getName, CourseVo::getName)
                .selectAs(Course::getTeacherId, CourseVo::getTeacherId)
                .selectAs(User::getName, CourseVo::getTeacherName)
                .innerJoin(User.class, User::getId, Course::getTeacherId)
                .like(StringUtils.isNotBlank(courseVo.getName()),Course::getName,courseVo.getName())
                .eq(courseVo.getTeacherId()!=null,Course::getTeacherId,courseVo.getTeacherId());
        IPage<CourseVo> courseVoIPage = courseMapper.selectJoinPage(new Page<CourseVo>(page, size), CourseVo.class, wrapper);
        if (courseVoIPage != null && courseVoIPage.getRecords().size() > 0)
            return Result.ok().data("courseList", courseVoIPage);
        return Result.ok().data("msg", "暂无数据").setCode(404);
    }

    @Override
    public Result getAllCourse() {
        MPJLambdaWrapper<Course> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAs(Course::getId, CourseVo::getId)
                .selectAs(Course::getName, CourseVo::getName)
                .selectAs(Course::getTeacherId, CourseVo::getTeacherId)
                .selectAs(User::getName, CourseVo::getTeacherName)
                .innerJoin(User.class, User::getId, Course::getTeacherId);
        List<CourseVo> courseVos = courseMapper.selectJoinList(CourseVo.class, wrapper);
        if (courseVos != null && courseVos.size() > 0)
            return Result.ok().data("courseList", courseVos);
        return Result.ok().data("msg", "暂无数据").setCode(404);
    }
}
